package cn.kgc.springboot05.controller;

import cn.kgc.springboot05.entity.User;
import cn.kgc.springboot05.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2023/1/3
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public String userList(Model model){
        List<User> users = userService.list();
        model.addAttribute("users", users);
        return "user";
    }

    @RequestMapping("add")
    public String addUser(User user){
        boolean save = userService.save(user);
        return "redirect:/user/list";
    }

    @RequestMapping("toEdit")
    public String toEdit(Integer id,Model model){
        User user = userService.getById(id);
        model.addAttribute("user", user);
        return "edit";
    }

    @RequestMapping("edit")
    public String editUser(User user){
        userService.updateById(user);
        return "redirect:/user/list";
    }

    @RequestMapping("delete")
    public String deleteUser(Integer id){
        userService.removeById(id);
        return "redirect:/user/list";
    }

}
